package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.Packing;
import cn.itcast.domain.cargo.PackingExample;
import cn.itcast.service.cargo.PackingService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.Map;

/**
 * 装箱service
 */
@Controller
@RequestMapping("/cargo/packing")
public class PackingController extends BaseController {

    @Reference
    private PackingService packingService;

    /**
     * 装箱前的操作  先去用报运单 (报运单要在exportController中进行修改 因为在那里会有一个校验-->校验是否能够多个包运单一起装箱)
     * 这里还要进行校验 对送过来的所有包运单的ids进行一个校验 (就是三个要求 装运港 运输港 还有就是运输方式)
     * @param exportIds
     * @return
     */
    @RequestMapping("/checkAdd")
    @ResponseBody
    public Map<String,Object> checkAdd(String exportIds) {
        //先校验报运单状态 (只有是 已报运状态才能进行下一步操作)
        Map<String,Object> map = packingService.checkStatus(exportIds);
        boolean success = (boolean) map.get("success");
        if (!success) {
            return map;
        }
        return packingService.checkAdd(exportIds);
    }

    /**
     * 去到装箱页面
     * @param exportIds
     * @return
     */
    @RequestMapping("/toAdd")
    public String toAdd(String exportIds) {
        Packing packing = packingService.toAddPacking(exportIds);
        request.setAttribute("packing",packing);
        return "cargo/packing/packing-add";
    }

    @RequestMapping("/toError")
    public String toError(String error) {
        request.setAttribute("error",error);
        return "/cargo/packing/packing-error";
    }

    @RequestMapping("/edit")
    public String edit(Packing packing) {
        packing.setCompanyId(getLoginCompanyId());
        packing.setCompanyName(getLoginCompanyName());
        if (StringUtils.isEmpty(packing.getPackingListId())) {
            packing.setCreateTime(new Date());
            packing.setCreateBy(getLoginUser().getId());
            packing.setCreateDept(getLoginUser().getDeptId());
            //保存
            packingService.save(packing);
        }
        return "redirect:/cargo/packing/list";
    }

    @RequestMapping("/list")
    public String list(
            @RequestParam(defaultValue = "1")Integer pageNum,
            @RequestParam(defaultValue = "5")Integer pageSize
    ) {
        //查询报运中的报运状态 来进行查询(查那些已经是报运状态的报运单)
        PackingExample packingExample = new PackingExample();
        packingExample.createCriteria().andCompanyIdEqualTo(getLoginCompanyId());
        PageInfo<Packing> pageInfo = packingService.findByPage(packingExample, pageNum, pageSize);
        request.setAttribute("pageInfo",pageInfo);
        return "/cargo/packing/packing-list";
    }

    @RequestMapping("/delete")
    public String delete(@RequestParam("id") String id) {
        Packing packing = packingService.selectByPrimaryKey(id);
        //草稿状态才能删除
        if (packing.getState() == 0) {
            packingService.delete(id);
        }
        return "redirect:/cargo/packing/list";
    }

    @RequestMapping("/toView")
    public String toView(@RequestParam("id")String packingIds) {
        Packing packing = packingService.selectByPrimaryKey(packingIds);
        request.setAttribute("packing", packing);
        return "cargo/packing/packing-view";
    }

}